﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Presentation.Commands;
using System.Collections.Generic;
using Scoreboard.Basement.Controls;
using System.Collections.ObjectModel;

namespace Scoreboard.Basement.ViewModels
{
	public class DialogViewModel : ViewModelBase
	{
		private DialogWindow _window;
		private bool _isOnInitialPanel = true;
		private bool _hasCloseButton;
		private string _initialTitle;
		private string _alternateTitle;
		private double _windowWidth;
		private ObservableCollection<UserCommand> _initialCommands;
		private ObservableCollection<UserCommand> _alternateCommands;

		public DialogViewModel()
		{
			_initialCommands = new ObservableCollection<UserCommand>();
			_alternateCommands = new ObservableCollection<UserCommand>();

			CloseWindowCommand = new DelegateCommand<object>(CloseWindowCommandHandler);
			_windowWidth = 400;
		}		

		public ICommand CloseWindowCommand { get; private set; }

		public bool HasCloseButton
		{
			get
			{
				return _hasCloseButton;
			}
			set
			{
				_hasCloseButton = value;
				FirePropertyChanged("HasCloseButton");
			}
		}

		public string Title 
		{
			get
			{
				return IsOnInitialPanel ? InitialTitle : AlternateTitle;
			}
		}

		public string InitialTitle
		{
			get
			{
				return _initialTitle;
			}
			set
			{
				_initialTitle = value;
				FirePropertyChanged("InitialTitle");
				FirePropertyChanged("Title");
			}
		}

		public string AlternateTitle
		{
			get
			{
				return _alternateTitle;
			}
			set
			{
				_alternateTitle = value;
				FirePropertyChanged("AlternateTitle");
				FirePropertyChanged("Title");
			}
		}

		public double WindowWidth
		{
			get
			{
				return _windowWidth;
			}
			set
			{
				_windowWidth = value;
				FirePropertyChanged("WindowWidth");
			}
		}

		public ObservableCollection<UserCommand> Commands
		{
			get
			{
				return IsOnInitialPanel ? InitialCommands : AlternateCommands;
			}
		}

		public ObservableCollection<UserCommand> InitialCommands 
		{
			get
			{
				return _initialCommands;
			}
		}

		public ObservableCollection<UserCommand> AlternateCommands
		{
			get
			{
				return _alternateCommands;
			}
		}

		public bool IsOnInitialPanel
		{
			get
			{
				return _isOnInitialPanel;
			}
			set
			{
				_isOnInitialPanel = value;
				FirePropertyChanged("IsOnInitialPanel");
				FirePropertyChanged("Commands");
				FirePropertyChanged("Title");
			}
		}

		public void AttachView(DialogWindow view)
		{
			_window = view;
		}

		protected void NavigateToInitialPanel()
		{
			_window.NavigateToInitialPanel();
			IsOnInitialPanel = true;
		}
		
		protected void NavigateToAlternatePanel()
		{
			_window.NavigateToAlternatePanel();
			IsOnInitialPanel = false;
		}

		protected void CloseWindow()
		{
			_window.Close();
		}

		private void CloseWindowCommandHandler(object parameter)
		{
			CloseWindow();
		}
	}
}
